//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BlackIborCouponPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletVolatilityStructure.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/IborCouponPricer.h>
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CBlackIborCouponPricer::CBlackIborCouponPricer (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^>^ v) : CIborCouponPricer(CBlackIborCouponPricer::typeid)
{
    COptionletVolatilityStructure^ _Cv;
    try
    {
#ifdef HANDLE
        _phBlackIborCouponPricer = NULL;
#endif
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^>::IsSome::get (v))
        {
            _Cv = safe_cast<COptionletVolatilityStructure^> (v->Value);
            _Cv->Lock();
        }
        Handle<QuantLib::OptionletVolatilityStructure>& _v = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^>::IsSome::get (v) ? static_cast<Handle<QuantLib::OptionletVolatilityStructure>&> (_Cv->GetHandle ()) : Handle<QuantLib::OptionletVolatilityStructure>()); //1
        _ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (new QuantLib::BlackIborCouponPricer ( _v ));
        SetIborCouponPricer (boost::dynamic_pointer_cast<QuantLib::IborCouponPricer> (*_ppBlackIborCouponPricer));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cv != nullptr) _Cv->Unlock();
    }
}
Cephei::QL::Cashflows::CBlackIborCouponPricer::CBlackIborCouponPricer (boost::shared_ptr<QuantLib::BlackIborCouponPricer>& childNative, Object^ owner) : CIborCouponPricer(CBlackIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborCouponPricer = NULL;
#endif
	_ppBlackIborCouponPricer = &childNative;
    _ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::IborCouponPricer> (*_ppBlackIborCouponPricer));
}
Cephei::QL::Cashflows::CBlackIborCouponPricer::CBlackIborCouponPricer (QuantLib::BlackIborCouponPricer& childNative, Object^ owner) : CIborCouponPricer(CBlackIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborCouponPricer = NULL;
#endif
	_ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (&childNative);
    _ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::IborCouponPricer> (*_ppBlackIborCouponPricer));
    _BlackIborCouponPricerOwner = owner;
    _IborCouponPricerOwner = owner;
}

Cephei::QL::Cashflows::CBlackIborCouponPricer::CBlackIborCouponPricer (CBlackIborCouponPricer^ copy) : CIborCouponPricer(CBlackIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborCouponPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (copy->GetShared());
        _ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::IborCouponPricer> (*_ppBlackIborCouponPricer));
    }
}
Cephei::QL::Cashflows::CBlackIborCouponPricer::CBlackIborCouponPricer (PLATFORM::Type^ t) : CIborCouponPricer(CBlackIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborCouponPricer = NULL;
#endif
	if (!t->IsSubclassOf(CBlackIborCouponPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CBlackIborCouponPricer::CBlackIborCouponPricer (QuantLib::Handle<QuantLib::BlackIborCouponPricer>& childNative, Object^ owner)  : CIborCouponPricer(CBlackIborCouponPricer::typeid)
{
	_phBlackIborCouponPricer = &childNative;
	_ppBlackIborCouponPricer = &static_cast<boost::shared_ptr<QuantLib::BlackIborCouponPricer>>(childNative.currentLink());
    _ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::IborCouponPricer> (*_ppBlackIborCouponPricer));
    _BlackIborCouponPricerOwner = owner;
}
Cephei::QL::Cashflows::CBlackIborCouponPricer::CBlackIborCouponPricer (QuantLib::Handle<QuantLib::BlackIborCouponPricer> childNative)  : CIborCouponPricer(CBlackIborCouponPricer::typeid)
{
	_phBlackIborCouponPricer = &childNative;
	_ppBlackIborCouponPricer = &static_cast<boost::shared_ptr<QuantLib::BlackIborCouponPricer>>(childNative.currentLink());
    _ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::IborCouponPricer> (*_ppBlackIborCouponPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CBlackIborCouponPricer::CBlackIborCouponPricer (QuantLib::BlackIborCouponPricer childNative)  : CIborCouponPricer(CBlackIborCouponPricer::typeid)
{
#ifdef HANDLE
	_phBlackIborCouponPricer = NULL;
#endif
	_ppBlackIborCouponPricer = new boost::shared_ptr<QuantLib::BlackIborCouponPricer> (new QuantLib::BlackIborCouponPricer (childNative));
    _ppIborCouponPricer = new boost::shared_ptr<QuantLib::IborCouponPricer> (boost::dynamic_pointer_cast<QuantLib::IborCouponPricer> (*_ppBlackIborCouponPricer));
}
#endif

Cephei::QL::Cashflows::CBlackIborCouponPricer::~CBlackIborCouponPricer ()
{
    if (_ppBlackIborCouponPricer != NULL)
    {
	    delete _ppBlackIborCouponPricer;
        _ppBlackIborCouponPricer = NULL;
    }
}
Cephei::QL::Cashflows::CBlackIborCouponPricer::!CBlackIborCouponPricer ()
{
    if (_ppBlackIborCouponPricer != NULL)
    {
	    delete _ppBlackIborCouponPricer;
    }
}
QuantLib::BlackIborCouponPricer& Cephei::QL::Cashflows::CBlackIborCouponPricer::GetReference ()
{
    if (_ppBlackIborCouponPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppBlackIborCouponPricer;
}
boost::shared_ptr<QuantLib::BlackIborCouponPricer>& Cephei::QL::Cashflows::CBlackIborCouponPricer::GetShared ()
{
    if (_ppBlackIborCouponPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppBlackIborCouponPricer;
}
QuantLib::BlackIborCouponPricer* Cephei::QL::Cashflows::CBlackIborCouponPricer::GetPointer ()
{
    if (_ppBlackIborCouponPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppBlackIborCouponPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackIborCouponPricer>& Cephei::QL::Cashflows::CBlackIborCouponPricer::GetHandle ()
{
	if (_phBlackIborCouponPricer == NULL)
	{
		_phBlackIborCouponPricer = new Handle<QuantLib::BlackIborCouponPricer> (*_ppBlackIborCouponPricer);
	}
	return *_phBlackIborCouponPricer;
}
#endif
bool Cephei::QL::Cashflows::CBlackIborCouponPricer::HasNative () 
{
	return (_ppBlackIborCouponPricer != NULL);
}

Double Cephei::QL::Cashflows::CBlackIborCouponPricer::CapletPrice (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackIborCouponPricer)->capletPrice ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CBlackIborCouponPricer::CapletRate (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBlackIborCouponPricer)->capletRate ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CBlackIborCouponPricer::FloorletPrice (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackIborCouponPricer)->floorletPrice ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CBlackIborCouponPricer::FloorletRate (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBlackIborCouponPricer)->floorletRate ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IBlackIborCouponPricer^ Cephei::QL::Cashflows::CBlackIborCouponPricer::Initialize (Cephei::QL::Cashflows::IFloatingRateCoupon^ coupon)
{
    CFloatingRateCoupon^ _Ccoupon;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccoupon = safe_cast<CFloatingRateCoupon^> (coupon);
        _Ccoupon->Lock();
        QuantLib::FloatingRateCoupon& _coupon = static_cast<QuantLib::FloatingRateCoupon&> (_Ccoupon->GetReference ()); 
    	(*_ppBlackIborCouponPricer)->initialize ( _coupon );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccoupon != nullptr) _Ccoupon->Unlock();
    }
}
Double Cephei::QL::Cashflows::CBlackIborCouponPricer::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackIborCouponPricer)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CBlackIborCouponPricer::SwapletRate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppBlackIborCouponPricer)->swapletRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IBlackIborCouponPricer^ Cephei::QL::Cashflows::CBlackIborCouponPricer_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^>^ v)
{
    return REFNEW CBlackIborCouponPricer ( v);
}
